package com.ux.example.particle
{
	import com.ux.effect.particle.Particle;
	import com.ux.manager.StageManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	
	public class ParticleSmokeExample extends Sprite
	{
		
		private var pList : Vector.<Particle>;
		private var numOfParticle : Number = 6;
		
		private var smokeSprite : Sprite;
		private var trailsBM:BitmapData;
		private var trailsContainer:Bitmap;
		private var point : Point = new Point(0,0);
		private var matrix:Array = new Array();
		private var blur : BlurFilter = new BlurFilter(8,8,1);
		private var cmatrix : ColorMatrixFilter;
		
		public function ParticleSmokeExample()
		{
			super();
			
			
			StageManager.stage = this.stage;
			
			smokeSprite = new Sprite();
			trailsBM = new BitmapData(550,400,false,0x000000); 
			trailsContainer = new Bitmap(trailsBM);
			addChild( trailsContainer );
			addChild( smokeSprite );
			
			
			smokeSprite.filters = [blur];
			
			
			matrix = matrix.concat([0.9, 0, 0, 0, 0]); // red
			matrix = matrix.concat([0, 0.1, 0, 0, 0]); // green
			matrix = matrix.concat([0, 0, 0.1, 0, 0]); // blue
			matrix = matrix.concat([0, 0, 0, 0.2, 0]); // alpha
			
			cmatrix = new ColorMatrixFilter( matrix );
			
			
			pList = new Vector.<Particle>();
			
			this.addEventListener( Event.ENTER_FRAME, onEnter );
			StageManager.stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown );
			StageManager.stage.addEventListener(MouseEvent.MOUSE_UP, onUp );
			StageManager.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove );
		}
		
		private function onDown( e:MouseEvent ):void
		{
			numOfParticle = 20;
		}
		
		private function onUp( e:MouseEvent ):void
		{
			numOfParticle = 1;
		}
		
		private function onEnter( e:Event ):void
		{
			trailsBM.draw(this);
			trailsBM.applyFilter(trailsBM, trailsBM.rect, point, cmatrix);
			
			addParticle();
			removeParticle();
			
			
			for(var i:Number = 0;i<pList.length;i++)
			{
				pList[i].update();
				pList[i].update();
			}
		}
		
		private function onMove( e:MouseEvent ):void
		{
			trailsBM.draw(this);
			trailsBM.applyFilter(trailsBM, trailsBM.rect, point, cmatrix);
			
			addParticle();
			removeParticle();
		}
		
		private function addParticle():void
		{
			var len:int = numOfParticle;
			for (var j:int = 0; j < len; j++) 
			{
				var p : Particle = new Particle( SmokeClass, smokeSprite, StageManager.stage.mouseX, StageManager.stage.mouseY );
				pList.push( p );
				
				p.setVel( randRange(-0.3,0.3), 0 );
				
				p.clip.alpha = 0.4;
				p.gravity = -0.05;
				p.shrink = 1.02;
				p.drag = 0.99;
			}
		}
		
		private function removeParticle():void
		{
			while( pList.length > 300 )
			{
				pList.shift().destroy();
			}
			
		}
		
		
		
		private function randRange(min:Number, max:Number) : Number
		{
			var randomNum:Number = (Math.random() * (max - min )) + min;
			return randomNum;
		}
	}
}






